package com.yyfax.ph.personal.settings;

import android.text.TextUtils;

import com.yyfax.common.CLog;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.IView;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterFactory;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class BankCardPresenter extends Presenter<BankCardModel, BankCardPresenter.View> {
	private static final String TAG = BankCardPresenter.class.getSimpleName();

	public static final String NEVER_BIND = "0"; // 未曾绑定过
	public static final String HAS_UNBINDED = "1"; // 绑定过, 但是已解绑
	public static final String HAS_BINDED = "2"; // 已绑定

	public static class Generator implements PresenterFactory<BankCardPresenter> {
		@Override
		public BankCardPresenter create() {
			return new BankCardPresenter();
		}
	}

	private View mView;
	private String mRequestPage;

	@Override
	public void attach(View view) {
		mView = view;
		mRequestPage = view.getClass().getName();
		mModel = new BankCardModel();
		EventAgent.register(this);
	}

	@Override
	public void detach() {
		mView = null;
		mModel = null;
		EventAgent.unregister(this);
	}

	public interface View extends IView {
		void getBankCardInfoSuccess(String name, String certId, String cardId, String bank, String bankCode);

		void getBankCardInfoFailed(String tips);
	}

	/**
	 * 获取银行卡信息
	 */
	public void getBindBankCardInfo() {
		execute(null, BankCardModel.MSG_GET_BANK_CARD, true, null, mRequestPage);
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void getBankCardInfoResult(BankCardModel.BankCardResult result) {
		if (mRequestPage.equals(result.requestPage)) {
			CLog.i(TAG, "getBankCardInfoResult>>" + result.toString());
			if (isResponseSuccess(result)) {
				String bindStatus = result.getContent().bindStatus;
				BankCardModel.Content.CardInfo cardInfo = result.getContent().cardInfo;
				if (HAS_BINDED.equals(bindStatus) || HAS_UNBINDED.equals(bindStatus)){
					if (null != cardInfo) {
						mView.getBankCardInfoSuccess(cardInfo.name, cardInfo.certId, cardInfo.cardId, cardInfo.bank, cardInfo.bankCode);
						return;
					}
				}
				mView.getBankCardInfoSuccess("", "", "", "", "");
			} else {
				mView.getBankCardInfoFailed(TextUtils.isEmpty(result.getMsg()) ? "网络连接失败, 请重试" : result.getMsg());
			}
		}
	}
}
